package net.wanho.shop.service.impl;

import net.wanho.shop.po.GoodsAttr;
import net.wanho.shop.mapper.GoodsAttrMapper;
import net.wanho.shop.service.GoodsAttrService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品-属性关联表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsAttrServiceImpl implements GoodsAttrService {

    private final GoodsAttrMapper goodsAttrMapper;

    /**
     * 商品-属性关联表分页查询
     * @param pageNum
     * @param pageSize
     * @param goodsAttr
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, GoodsAttr goodsAttr) {
        if(ObjectUtil.isNull(goodsAttr)){
            goodsAttr = new GoodsAttr();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsAttr> goodsAttrList = goodsAttrMapper.select(goodsAttr);
        return new PageInfo(goodsAttrList);
    }

    /**
     * 根据主键查询商品-属性关联表
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public GoodsAttr findById(Serializable id) {
        return goodsAttrMapper.selectById(id);
    }

    /**
     * 商品-属性关联表新增
     * @param goodsAttr
     */
    @Override
    public void add(GoodsAttr goodsAttr) {
        goodsAttrMapper.insert(goodsAttr);
    }

    /**
     * 修改商品-属性关联表
     * @param goodsAttr
     */
    @Override
    public void modify(GoodsAttr goodsAttr) {
        goodsAttrMapper.update(goodsAttr);
    }

    /**
     * 根据主键删除商品-属性关联表
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if(((String)id).contains(",")){
            goodsAttrMapper.deleteByIds(((String)id).split(","));
        }else {
            goodsAttrMapper.deleteById(id);
        }
    }
}
